package www.way2inbox.settings;

import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

import com.way2sms.way2inbox.R;


public class SettingsNotificationActivity extends PreferenceActivity implements
		UncaughtExceptionHandler {
	SharedPreferences preferences = null;
	Preference p,vibrate;

	@SuppressWarnings("unused")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		try {

			UncaughtExceptionHandler mDfltExceptionHandler = Thread
					.getDefaultUncaughtExceptionHandler();
			Thread.setDefaultUncaughtExceptionHandler(this);
			((TextView) findViewById(R.id.title))
					.setText("Notification Settings");
			addPreferencesFromResource(R.xml.settings_notifications);
			p = getPreferenceScreen().findPreference(
					ApplicationConstants.SettingsPreferenceKeys.RINGTONE);
			preferences = PreferenceManager
					.getDefaultSharedPreferences(getBaseContext());
			String name = preferences.getString(
					ApplicationConstants.SettingsPreferenceKeys.RINGTONE,
					"Default Ringtone");
			p.setSummary(ringtoneToName(SettingsNotificationActivity.this, name));
			p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
				public boolean onPreferenceChange(Preference preference,
						Object newValue) {
					// Never hits here!
					String v = (String) newValue;
					preference.setSummary(ringtoneToName(
							SettingsNotificationActivity.this, v));
					return true;
				}

			});
            vibrate=getPreferenceScreen().findPreference(ApplicationConstants.SettingsPreferenceKeys.VIBRATE_PATTERN);
            preferences = PreferenceManager
                    .getDefaultSharedPreferences(getBaseContext());
            String viberpattern = preferences.getString(
                    ApplicationConstants.SettingsPreferenceKeys.VIBRATE_PATTERN,
                   "Short");
            vibrate.setSummary(viberpattern);
            vibrate.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                public boolean onPreferenceChange(Preference preference,
                                                  Object newValue) {
                    // Never hits here!
                    String v = (String) newValue;
                    preference.setSummary(v);
                    return true;
                }

            });
            p=getPreferenceScreen().findPreference(ApplicationConstants.SettingsPreferenceKeys.VIBRATE);
            p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                public boolean onPreferenceChange(Preference preference,
                                                  Object newValue) {
                    // Never hits here!
                    boolean b = (Boolean) newValue;
                    if (b) {
                        vibrate.setEnabled(true);
                    } else {
                        vibrate.setEnabled(false);
                    }
                    return true;
                }

            });
		} catch (Exception e) {
			String stackTrace = Log.getStackTraceString(e);
			System.out.println("stackTrace" + stackTrace);
			DeviceDetailsClass dd = new DeviceDetailsClass(this);
			dd.sendEmail(stackTrace);
		}

	}

	public CharSequence ringtoneToName(Context context,
			String strRingtonePreference) {
		Uri ringtoneUri = Uri.parse(strRingtonePreference);
		Ringtone ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
		String name = ringtone.getTitle(getBaseContext());
		return name;
	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex) {

	}
}
